package com.jimikongjian.service;

import com.jimikongjian.dao.IAlbum;
import com.jimikongjian.models.Album;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.IAlbumService;
import com.jimikongjian.service.base.IRoomService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/13.
 */
@Service("albumService")
public class AlbumService implements IAlbumService{

    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iAlbumImpl")
    private IAlbum iAlbumImpl;

    @Autowired
    @Qualifier("roomService")
    private IRoomService roomService;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    /**
     * 创建一个相册
     *
     * @param album
     * @return
     */
    public ResponseMessage createAlbum(Album album) {
        if (album.getUserId() > 0
                && album.getUserNick() != ""
                && album.getRoomId() > 0
                && album.getRoomName() != ""
                && album.getAlbumTitle() != ""
                && album.getAlbumDesc() != ""){
//           这里应该考虑为新建的相册添加封面的信息
            iAlbumImpl.insertAlbum(album);
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过相册的id查询相册
     *
     * @param id
     * @return
     */
    public ResponseMessage selectAlbumById(int id) {
        if (id > 0){
            Album album = iAlbumImpl.getAlbumById(id);
            if (album != null){
                return responseMessageService.makeMessage(album, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ALBUM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过空间的id，查询该空间下的所有的相册
     *
     * @param roomId
     * @return
     */
    public ResponseMessage selectAlbumByRoomId(int roomId, int start, int length) {
        if (roomId > 0){
            if (roomService.hasRoom(roomId)){
                List<Album> albumList = iAlbumImpl.getAlbumListByRoomId(roomId, start, length);
                return responseMessageService.makeMessage(albumList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过用户的id号，查询该用户创建的所有的相册
     *
     * @param userId
     * @return
     */
    public ResponseMessage selectAlbumByUserId(int userId) {
        if (userId > 0){
            if (userService.hasUser(userId)){
                List<Album> albumList = iAlbumImpl.getAlbumListByUserId(userId);
                return responseMessageService.makeMessage(albumList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 更新相册信息
     *
     * @param album
     * @return
     */
    public ResponseMessage updateAlbum(Album album) {
        if (album.getId() > 0
                && album.getAlbumTitle() != ""
                && album.getAlbumDesc() != ""){
            iAlbumImpl.updateAlbum(album);
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 是否含有该相册
     *
     * @param id
     * @return
     */
    public boolean hasAlbum(int id) {
        if (id > 0){
            if (iAlbumImpl.getAlbumById(id) != null){
                return true;
            }
        }
        return false;
    }
}
